package cn.csc.module.shop.controller.admin.audit;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.audit.vo.*;
import cn.csc.module.shop.convert.audit.ShopAuditConvert;
import cn.csc.module.shop.dal.dataobject.audit.ShopAuditDO;
import cn.csc.module.shop.service.audit.ShopAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 店铺审核记录")
@RestController
@RequestMapping("/shop/audit")
@Validated
public class ShopAuditController {

    @Resource
    private ShopAuditService auditService;

    @PostMapping("/create")
    @ApiOperation("创建店铺审核记录")
    @PreAuthorize("@ss.hasPermission('shop:audit:create')")
    public CommonResult<BigInteger> createAudit(@Valid @RequestBody ShopAuditCreateReqVO createReqVO) {
        return success(auditService.createAudit(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新店铺审核记录")
    @PreAuthorize("@ss.hasPermission('shop:audit:update')")
    public CommonResult<Boolean> updateAudit(@Valid @RequestBody ShopAuditUpdateReqVO updateReqVO) {
        auditService.updateAudit(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得店铺审核记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:audit:query')")
    public CommonResult<ShopAuditRespVO> getAudit(@RequestParam("id") BigInteger id) {
        ShopAuditDO audit = auditService.getAudit(id);
        return success(ShopAuditConvert.INSTANCE.convert(audit));
    }

    @GetMapping("/list")
    @ApiOperation("获得店铺审核记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:audit:query')")
    public CommonResult<List<ShopAuditRespVO>> getAuditList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopAuditDO> list = auditService.getAuditList(ids);
        return success(ShopAuditConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listByShopId")
    @ApiOperation("获得店铺审核记录列表")
    @ApiImplicitParam(name = "shopId", value = "编号列表", required = true, example = "102434213514512131", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:audit:query')")
    public CommonResult<List<ShopAuditRespVO>> listByShopId(@RequestParam("shopId") BigInteger shopId) {
        List<ShopAuditDO> list = auditService.getAuditListByShopId(shopId);
        return success(ShopAuditConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得店铺审核记录分页")
    @PreAuthorize("@ss.hasPermission('shop:audit:query')")
    public CommonResult<PageResult<ShopAuditRespVO>> getAuditPage(@Valid ShopAuditPageReqVO pageVO) {
        PageResult<ShopAuditDO> pageResult = auditService.getAuditPage(pageVO);
        return success(ShopAuditConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出店铺审核记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:audit:export')")
    @OperateLog(type = EXPORT)
    public void exportAuditExcel(@Valid ShopAuditExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopAuditDO> list = auditService.getAuditList(exportReqVO);
        // 导出 Excel
        List<ShopAuditExcelVO> datas = ShopAuditConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺审核记录.xls", "数据", ShopAuditExcelVO.class, datas);
    }

}
